const mysql = require("mysql");
const DATABASE = "international";
module.exports = {
  pool: null,
  //数据库配置
  config: {
    host: "localhost",
    port: "3306",
    user: "root",
    password: "Gqm19990116..",
    database: DATABASE,
  },
  createConnectPool: function () {
    this.pool = mysql.createPool(this.config);
  },
  sqlConnect: function (sql, sqlArr, callBack) {
    this.pool.getConnection(function (err, conn) {
      if (err) {
        console.log("连接失败", err);
        return;
      }
      conn.query(sql, sqlArr, callBack);
      conn.release();
    });
  },
  SySqlConnect: function (sySql, sqlArr) {
    return new Promise((resolve, reject) => {
      this.pool.getConnection(function (err, conn) {
        if (err) {
          reject(err);
        } else {
          conn.query(sySql, sqlArr, (err, data) => {
            if (err) {
              reject(err);
            } else {
              resolve(data);
            }
            conn.release();
          });
        }
      });
    }).catch((err) => {
      console.log(err);
    });
  },
};
